package io.renren.modules.echarts.service.Impl;

import io.renren.common.utils.Query;
import io.renren.modules.echarts.dao.AttendenceCahrtsDao;
import io.renren.modules.echarts.entity.*;
import io.renren.modules.echarts.service.AttendenceChartsService;
import io.renren.modules.jxgk.common.ZTreeNode;
import io.renren.modules.jxgk.dao.CollegeInfoDao;
import io.renren.modules.jxgk.entity.CollegeInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class AttendenceChartsServiceImpl implements AttendenceChartsService {
    @Autowired
    AttendenceCahrtsDao attendenceCahrtsDao;
    @Autowired
    CollegeInfoDao collegeInfoDao;
    @Autowired
    TeacherPortraitServiceImpl teacherPortraitService;
    @Override
    public Map<String,Object> getAttendanceClassRankingFirstTen(Map<String, Object> params) {
        Map<String,Object> map=new HashMap<>();
        List<String> classNamesFirstTen=new ArrayList<>();
        List<Integer> rateFirstTen=new ArrayList<>();
        List<String> classNamesEndtTen=new ArrayList<>();
        List<Integer> rateEndTen=new ArrayList<>();
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<AttendenceRate> list=attendenceCahrtsDao.getAttendanceClassRanking(params);
        int j=10;
        if(j>list.size())
        {
            j=list.size();
        }
        for(int i=0;i<j;i++)
        {
            classNamesEndtTen.add(list.get(i).getRateName());
            rateEndTen.add(list.get(i).getRate());
        }
        for(int i=(list.size()-1);i>=(list.size()-j);i--)
        {
            classNamesFirstTen.add(list.get(i).getRateName());
            rateFirstTen.add(list.get(i).getRate());
        }
        map.put("classNamesFirstTen",classNamesFirstTen);
        map.put("rateFirstTen",rateFirstTen);
        map.put("classNamesEndtTen",classNamesEndtTen);
        map.put("rateEndTen",rateEndTen);
        return map;
    }

    @Override
    public Map<String, Object> getAttendanceCollageRanking(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        Map<String,Object> map=new HashMap<>();
        List<String> collageName=new ArrayList<>();
        List<Integer> collageRate=new ArrayList<>();
        List<AttendenceRate> list=attendenceCahrtsDao.getAttendanceCollageRanking(params);
        for(AttendenceRate a:list)
        {
            collageName.add(a.getRateName());
            collageRate.add(a.getRate());
        }
        map.put("collageName",collageName);
        map.put("collageRate",collageRate);
        return map;
    }

    @Override
    public Map<String,List> getAttendanceCollegeHorizontalComparison(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);

        String collegeNames=((String)params.get("collegeName"));

        String college[]=collegeNames.split(",");
        List legend=new ArrayList();
        List data=new ArrayList();
        List xAxis=new ArrayList<>();
        Map<String,List> map=new HashMap<>();
        //如果为空
        if(college[0].equals(""))
        {
            //查的是全校所有的数据
            List<AttendanceDate> list=attendenceCahrtsDao.getAttendanceCollegeHorizontalComparison(params);
            List datas=new ArrayList();
            EchartsDate echartsDate = new EchartsDate();
            echartsDate.setName("全校");
            echartsDate.setType("line");
            for (AttendanceDate a : list) {
                xAxis.add(a.getName());
                datas.add(a.getValue());
            }
            echartsDate.setData(datas);
            data.add(echartsDate);
            legend.add("全校");
        }
        else {
            for (int i = 0; i < college.length; i++) {
                college[i]=college[i].replace("\'","");
                legend.add(college[i]);
                params.remove("collegeName");
                params.put("collegeName", college[i]);
                List<AttendanceDate> list = attendenceCahrtsDao.getAttendanceCollegeHorizontalComparison(params);
                EchartsDate echartsDate = new EchartsDate();
                echartsDate.setName(college[i]);
                echartsDate.setType("line");
                List datas=new ArrayList();
                if (xAxis.size() == 0) {
                    for (AttendanceDate a : list) {
                        xAxis.add(a.getName());
                    }
                }
                if(list.size()!=0) {
                    for (AttendanceDate a : list) {
                        datas.add(a.getValue());
                    }
                    echartsDate.setData(datas);
                    data.add(echartsDate);
                }
            }
        }
        map.put("legend", legend);
        map.put("xAxis", xAxis);
        map.put("data", data);
        return  map;
    }

    @Override
    public Map<String, List> getAttendanceClassHorizontalComparison(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        String classNames=((String)params.get("className"));
        String className[]=classNames.split(",");
        List legend=new ArrayList();
        List data=new ArrayList();
        List xAxis=new ArrayList<>();
        Map<String,List> map=new HashMap<>();
            if(className[0].equals(""))
        {
            List<AttendanceDate> list=attendenceCahrtsDao.getAttendanceClassHorizontalComparison(params);
            List datas=new ArrayList();
            EchartsDate echartsDate = new EchartsDate();
            echartsDate.setName("全校");
            echartsDate.setType("line");
            for (AttendanceDate a : list) {
                xAxis.add(a.getName());
                datas.add(a.getValue());
            }
            echartsDate.setData(datas);
            data.add(echartsDate);
        }
        else {
            for (int i = 0; i < className.length; i++) {

                params.remove("className");
                params.put("className", className[i]);
                List<AttendanceDate> list = attendenceCahrtsDao.getAttendanceClassHorizontalComparison(params);
                EchartsDate echartsDate = new EchartsDate();
                echartsDate.setName(className[i]);
                echartsDate.setType("line");
                List datas=new ArrayList();
                if (xAxis.size() == 0) {
                    for (AttendanceDate a : list) {
                        xAxis.add(a.getName());
                    }
                }
                    if(list.size()!=0) {
                    for (AttendanceDate a : list) {
                        datas.add(a.getValue());
                    }
                    echartsDate.setData(datas);
                    legend.add(className[i]);
                    data.add(echartsDate);
                }
            }
        }
        map.put("legend", legend);
        map.put("xAxis", xAxis);
        map.put("data", data);
        return  map;
    }

    @Override
    public List<ZTreeNode> getClassTree() {

        List<CollegeInfoEntity> cie=collegeInfoDao.getAllStudentCollege2();
        List<ZTreeNode> zTreeNodeList= new ArrayList<>();
        for(CollegeInfoEntity c:cie)
        {
            ZTreeNode zTreeNode=new ZTreeNode();
            zTreeNode.setMenuId(c.getCollegeName());
            zTreeNode.setName(c.getCollegeName());
            zTreeNode.isNocheck(true);
            zTreeNodeList.add(zTreeNode);
            Map map=new HashMap();
            map.put("college",c.getCollegeName());
            List<String> professionList=collegeInfoDao.getProfessionByCollege(map);
            for(String p:professionList)
            {
                ZTreeNode zTreeNode1=new ZTreeNode();
                zTreeNode1.setMenuId(p);
                zTreeNode1.setName(p);
                zTreeNode1.setParentId(c.getCollegeName());
                zTreeNode1.setParentName(c.getCollegeName());
                zTreeNode1.isNocheck(true);

                zTreeNodeList.add(zTreeNode1);
                map.put("profession",p);
                List<String> classList=collegeInfoDao.getClassByCollege(map);
                for(String className:classList)
                {
                    ZTreeNode zTreeNode2=new ZTreeNode();
                    zTreeNode2.setMenuId(className);
                    zTreeNode2.setName(className);
                    zTreeNode2.isNocheck(false);
                    zTreeNode2.setParentId(p);
                    zTreeNode2.setParentName(p);
                    zTreeNodeList.add(zTreeNode2);
                }
            }
        }
        return zTreeNodeList;
    }

    @Override
    public List<StudentAttendenceClassRate> getStudentsAttendanceList(Query query) {
        return attendenceCahrtsDao.getStudentsAttendanceList(query);
    }

    @Override
    public int getStudentsAttendanceTotal(Query query) {
        return attendenceCahrtsDao.getStudentsAttendanceTotal(query);
    }

    @Override
    public Map<String, Object> getAttendanceProfessionRankingFirstTen(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        Map<String,Object> map=new HashMap<>();
        List<String> classNamesFirstTen=new ArrayList<>();
        List<Integer> rateFirstTen=new ArrayList<>();
        List<String> classNamesEndtTen=new ArrayList<>();
        List<Integer> rateEndTen=new ArrayList<>();
        List<AttendenceRate> list=attendenceCahrtsDao.getAttendanceProfessionRanking(params);
        int j=10;
        if(j>list.size())
        {
            j=list.size();
        }
        for(int i=0;i<j;i++)
        {
            classNamesEndtTen.add(list.get(i).getRateName());
            rateEndTen.add(list.get(i).getRate());
        }
        for(int i=(list.size()-1);i>=(list.size()-j);i--)
        {
            classNamesFirstTen.add(list.get(i).getRateName());
            rateFirstTen.add(list.get(i).getRate());
        }
        map.put("classNamesFirstTen",classNamesFirstTen);
        map.put("rateFirstTen",rateFirstTen);
        map.put("classNamesEndtTen",classNamesEndtTen);
        map.put("rateEndTen",rateEndTen);
        return map;
    }

    @Override
    public List<ZTreeNode> getProfessionTree() {
        List<CollegeInfoEntity> cie=collegeInfoDao.getAllStudentCollege2();
        List<ZTreeNode> zTreeNodeList= new ArrayList<>();
        for(CollegeInfoEntity c:cie)
        {
            ZTreeNode zTreeNode=new ZTreeNode();
            zTreeNode.setMenuId(c.getCollegeName());
            zTreeNode.setName(c.getCollegeName());
            zTreeNode.isNocheck(true);
            zTreeNodeList.add(zTreeNode);
            Map map=new HashMap();
            map.put("college",c.getCollegeName());
            List<String> professionList=collegeInfoDao.getProfessionByCollege(map);
            for(String p:professionList)
            {
                ZTreeNode zTreeNode1=new ZTreeNode();
                zTreeNode1.setMenuId(p);
                zTreeNode1.setName(p);
                zTreeNode1.setParentId(c.getCollegeName());
                zTreeNode1.setParentName(c.getCollegeName());
                zTreeNode1.isNocheck(true);

                zTreeNodeList.add(zTreeNode1);
                map.put("profession",p);
            }
        }
        return zTreeNodeList;
    }

    @Override
    public Map<String, List> getAttendanceProfessionHorizontalComparison(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);

        String classNames=((String)params.get("profession"));
        String className[]=classNames.split(",");
        List legend=new ArrayList();
        List data=new ArrayList();
        List xAxis=new ArrayList<>();
        Map<String,List> map=new HashMap<>();
        if(className[0].equals(""))
        {
            List<AttendanceDate> list=attendenceCahrtsDao.getAttendanceClassHorizontalComparison(params);
            List datas=new ArrayList();
            EchartsDate echartsDate = new EchartsDate();
            echartsDate.setName("全校");
            echartsDate.setType("line");
            for (AttendanceDate a : list) {
                xAxis.add(a.getName());
                datas.add(a.getValue());
            }
            echartsDate.setData(datas);
            data.add(echartsDate);
        }
        else {
            for (int i = 0; i < className.length; i++) {

                params.remove("profession");
                params.put("profession", className[i]);
                List<AttendanceDate> list = attendenceCahrtsDao.getAttendanceProfessionHorizontalComparison(params);
                EchartsDate2 echartsDate = new EchartsDate2();
                echartsDate.setName(className[i]);
                echartsDate.setType("line");
                List datas=new ArrayList();
                if (xAxis.size() == 0) {
                    for (AttendanceDate a : list) {
                        xAxis.add(a.getName());
                    }
                }
                if(list.size()!=0) {
                    for (AttendanceDate a : list) {
                        datas.add(a.getValue());
                    }
                    echartsDate.setData(datas);
                    legend.add(className[i]);
                    data.add(echartsDate);
                }
            }
        }
        map.put("legend", legend);
        map.put("xAxis", xAxis);
        map.put("data", data);
        return  map;
    }

    @Override
    public List<ZTreeNode> getProfessionTreeByCollege(String college) {
        CollegeInfoEntity collegeInfoEntity=new CollegeInfoEntity();
        collegeInfoEntity.setCollegeName(college);
        List<CollegeInfoEntity> cie=new ArrayList<>();
        cie.add(collegeInfoEntity);
        List<ZTreeNode> zTreeNodeList= new ArrayList<>();
        for(CollegeInfoEntity c:cie)
        {
            ZTreeNode zTreeNode=new ZTreeNode();
            zTreeNode.setMenuId(c.getCollegeName());
            zTreeNode.setName(c.getCollegeName());
            zTreeNode.isNocheck(true);
            zTreeNodeList.add(zTreeNode);
            Map map=new HashMap();
            map.put("college",c.getCollegeName());
            List<String> professionList=collegeInfoDao.getProfessionByCollege(map);
            for(String p:professionList)
            {
                ZTreeNode zTreeNode1=new ZTreeNode();
                zTreeNode1.setMenuId(p);
                zTreeNode1.setName(p);
                zTreeNode1.setParentId(c.getCollegeName());
                zTreeNode1.setParentName(c.getCollegeName());
                zTreeNode1.isNocheck(true);

                zTreeNodeList.add(zTreeNode1);
                map.put("profession",p);
            }
        }
        return zTreeNodeList;    }

    @Override
    public Map  studentAttendanceRate(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
       List<TeacherEvaltionPieEntity> list=  attendenceCahrtsDao.studentAttendanceRate(params);
       List<String> legend=new ArrayList<>();
       List<TeacherEvaltionPieEntity> series=new ArrayList<>();
        for(TeacherEvaltionPieEntity t:list)
        {
            if(t.getName().equals("出勤"))
            {
                series.add(t);
                legend.add(t.getName());
            }
        }
       for(TeacherEvaltionPieEntity t:list)
       {
           if(t.getName()!=null&&!t.getName().equals("出勤"))
           {
               series.add(t);
               legend.add(t.getName());
           }
       }
       Map map=new HashMap();
       map.put("legend",legend);
        map.put("series",series);
        return map;
    }

    @Override
    public Map<String, List> studentAttendanceRateTimeLine(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        List<TeacherEvaltionPieEntity> classDiaryList=attendenceCahrtsDao.studentAttendanceRateTimeLine(params);
        List<String> xData=new ArrayList<>();
        List<TeacherEvaltionPieEntity> series=new ArrayList<>();
        for(TeacherEvaltionPieEntity t:classDiaryList)
        {
            if(t.getName()!=null)
            {
                series.add(t);
                xData.add(t.getName());
            }
        }
        Map map=new HashMap();
        map.put("xData",xData);
        map.put("seriesData",series);
            return map;
    }

    @Override
    public List<String> getProfessionByCollege(Map<String, Object> params) {
        return collegeInfoDao.getProfessionByCollege2(params);

    }

    @Override
    public List<ZTreeNode> getClassTreeByCollege(Map<String, Object> params) {

        List<ZTreeNode> zTreeNodeList= new ArrayList<>();
            ZTreeNode zTreeNode=new ZTreeNode();
            zTreeNode.setMenuId(params.get("collegeName").toString());
            zTreeNode.setName(params.get("collegeName").toString());
            zTreeNode.isNocheck(true);
            zTreeNodeList.add(zTreeNode);
            Map map=new HashMap();
            map.put("college",params.get("collegeName").toString());
            List<String> professionList=collegeInfoDao.getProfessionByCollege(map);
            for(String p:professionList)
            {
                ZTreeNode zTreeNode1=new ZTreeNode();
                zTreeNode1.setMenuId(p);
                zTreeNode1.setName(p);
                zTreeNode1.setParentId(params.get("collegeName").toString());
                zTreeNode1.setParentName(params.get("collegeName").toString());
                zTreeNode1.isNocheck(true);

                zTreeNodeList.add(zTreeNode1);
                map.put("profession",p);
                List<String> classList=collegeInfoDao.getClassByCollege(map);
                for(String className:classList)
                {
                    ZTreeNode zTreeNode2=new ZTreeNode();
                    zTreeNode2.setMenuId(className);
                    zTreeNode2.setName(className);
                    zTreeNode2.isNocheck(false);
                    zTreeNode2.setParentId(p);
                    zTreeNode2.setParentName(p);
                    zTreeNodeList.add(zTreeNode2);
                }
        }
        return zTreeNodeList;
    }

    @Override
    public List<ZTreeNode> getClassTreeByGrade(Map<String, Object> params) {
        List<ZTreeNode> zTreeNodeList= new ArrayList<>();
        ZTreeNode zTreeNode=new ZTreeNode();
//        zTreeNode.setMenuId(params.get("collegeName").toString());
//        zTreeNode.setName(params.get("collegeName").toString());
//        zTreeNode.isNocheck(true);
//        zTreeNodeList.add(zTreeNode);
        Map map=new HashMap();
        map.put("collegeName",params.get("collegeName").toString());
        List<String> gradeList=collegeInfoDao.getgradeListByCollege(map);
        for(String p:gradeList)
        {
            ZTreeNode zTreeNode1=new ZTreeNode();
            zTreeNode1.setMenuId(p);
            zTreeNode1.setName(p);
            //zTreeNode1.setParentId(params.get("collegeName").toString());
            //zTreeNode1.setParentName(params.get("collegeName").toString());
            zTreeNode1.isNocheck(true);

            zTreeNodeList.add(zTreeNode1);
            map.put("grade",p);
            List<String> classList=collegeInfoDao.getClassByGrade(map);
            for(String className:classList)
            {
                ZTreeNode zTreeNode2=new ZTreeNode();
                zTreeNode2.setMenuId(className);
                zTreeNode2.setName(className);
                zTreeNode2.isNocheck(false);
                zTreeNode2.setParentId(p);
                zTreeNode2.setParentName(p);
                zTreeNodeList.add(zTreeNode2);
            }
        }
        return zTreeNodeList;
    }

}
